const SpiderBaseClass = require('./SpiderBaseClass');

const listSize = 20;//列表页面有多少个主播信息
const referer = '';

class SpiderZhanQi extends SpiderBaseClass {

  static firstListPage() {
    return 1;
  }

  /**
   * 获取其它页码的列表页面
   * @param page
   */
  static async getListPage(page) {
    const res = await SpiderZhanQi.get(SpiderZhanQi.listPageUrl(page),
      {dataType: 'json', headers: SpiderZhanQi.headers(referer)});

    const list = res.data.data.rooms.map(room => {
      let gender = 0;
      if (room.gender === '1')//战旗1是女，2是男
        gender = 2;
      else if (room.gender === '2')
        gender = 1;
      return {
        anchor: {
          room: room.code,
          name: room.nickname,
          gender,
          avatar: room.avatar,
          // 战旗的头像，需要在头像网址结尾添加-big，例如:
          // https://img2.zhanqi.tv/avatar/2c/494/21526903_1518493221.jpg-big
        },
        type: room.newGameName,
        data: {
          num_watch: SpiderZhanQi.toNumber(room.online),
          num_live: 0,
          num_follower: 0,
          num_gift: 0,
        },
      };
    });
    return {
      list,
      page,
      getMoreInfo: true,
      nextPage: page + 1,
      hasNextPage: list.length >= listSize,
    };
  }

  /**
   * 获取直播间信息
   * @param room
   */
  static async getRoom(room) {
    const res = await SpiderZhanQi.get(
      `https://m.zhanqi.tv/api/static/v2.1/room/domain/${room.anchor.room}.json`,
      {
        dataType: 'json',
        headers: SpiderZhanQi.headers('https://m.zhanqi.tv/' + room.anchor.room),
      });
    room.data.num_gift = SpiderZhanQi.toNumber(
      res.data.data.anchorAttr.hots.fight);//战斗力数值
    room.data.num_follower = SpiderZhanQi.toNumber(res.data.data.follows);
  }

  /**
   * 生成列表页面的网址
   * @param page
   * @return string
   */
  static listPageUrl(page) {
    return 'https://www.zhanqi.tv/api/static/v2.1/live/list/20/' + page +
      '.json';
  }

  /**
   * 生成直播间信息的网址
   * @param id
   * @return string
   */
  static roomUrl(id) {
  }

}

module.exports = SpiderZhanQi;